package mobi.hami.huapin.controller;

import io.swagger.annotations.*;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.HuaPinOutPutDTO;
import mobi.hami.huapin.dto.HuaPinParamDTO;
import mobi.hami.huapin.model.*;
import mobi.hami.huapin.service.*;
import mobi.hami.huapin.service.impl.HuapinOrderServiceImpl;
import mobi.hami.huapin.util.BeanUtil;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.UserDataHandler;
import sun.util.resources.cldr.aa.CalendarData_aa_ER;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Create by 小峰 on 2018/8/24.
 */
@RestController
@RequestMapping(value = "/huapin", produces = "application/json")
@Api(description = "花拼－拼团信息")
public class HuaPinController {

    @Autowired
    private UserCouponService userCouponService;
    @Autowired
    private HuapinOrderService huapinOrderService;
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private HuaPinService huaPinService;

    @RequestMapping(value = "/couponlist.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户Id获取优惠券List", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户Id", example = "1", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result getCouponByUserId(Integer userId) {
        return Results.successWithData(userCouponService.getCouponByUserId(userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/savehuapin.do", method = RequestMethod.POST)
    @ApiOperation(value = "保存花拼信息处理", notes = " ")
    public Result saveHuapinInfo(@RequestBody @ApiParam(value = " ") HuaPinParamDTO huaPinParamDTO) throws Exception {

        if(huaPinParamDTO.getHuapin_id()>0)
        {

            HuaPin huaPin=huaPinService.selectOnbyId(huaPinParamDTO.getHuapin_id());
            if(huaPin!=null && huaPin.getEnd_time().before(new Date()))
            {
                return Results.successWithData("拼团已结束！", BaseEnums.FAILURE.code(), BaseEnums.FAILURE.desc());
            }
//            UserOrder userOrder= new UserOrder();
//            userOrder.setHuapin_id(huaPinParamDTO.getHuapin_id());
//            userOrder.setUser_id(huaPinParamDTO.getUser_id());
//            userOrder.setOrder_status(2);
//            userOrder=userOrderService.findOneByModel(userOrder);
//            if(userOrder!=null)
//            {
//                return Results.successWithData("您已经参加过该拼团！", BaseEnums.FAILURE.code(), BaseEnums.FAILURE.desc());
//            }
        }
        Map<String, String> stringMap = huapinOrderService.saveHuaPinOrder(huaPinParamDTO);
        if(stringMap==null || stringMap.isEmpty()){
            return Results.successWithData("订单有误!", BaseEnums.FAILURE.code(), BaseEnums.FAILURE.desc());
        }else{
            return Results.successWithData(stringMap, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
    }

    @RequestMapping(value = "/gethuapinbyid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据花拼id，获取花拼信息", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "花拼Id", example = "5", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result getHuapinById(Integer id) {
        return Results.successWithData(huapinOrderService.getOneByHuapinId(id), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/printorder.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据花拼id，获取花拼信息,用于打印，返回69码", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "花拼Id", example = "5", paramType = "query", required = true, dataType = "Integer"),
            }
    )
    public Result printorder(Integer id) {
        return Results.successWithData(huapinOrderService.getOneForPrint(id), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getHuapinPageByAddressId.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据地址id，获取该地址下所以正在拼购的团", notes = "{addressId:地址Id,pageIndex:1,pageSize:10}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{startPageNum:开始页数,totalNum:总条数,pageSize:每页显示条数," +
                    "totalPageNum:总页数,List[{}]}")
    })
    public Result getHuapinPageByAddressId(@RequestBody Map<String, Object> map){

        return Results.successWithData(huapinOrderService.getListByAddressId(map), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getHuapinPageByAddressIdNew.do", method = RequestMethod.POST)
    @ApiOperation(value = "(新,小程序使用)根据地址id，获取该地址下所以正在拼购的团", notes = "{addressId:地址Id,pageIndex:1,pageSize:10}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{startPageNum:开始页数,totalNum:总条数,pageSize:每页显示条数," +
                    "totalPageNum:总页数,List[{}]}")
    })
    public Result grtHuapinPageByAddressIdNew(@RequestBody Map<String, Object> map){
        return Results.successWithData(huaPinService.getHuaPinByAddressId(map), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/gethuapinbyuserid.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据用户id，获取该用户下所有拼购的团", notes = "{userId:用户Id,pageIndex:1,pageSize:10}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{startPageNum:开始页数,totalNum:总条数,pageSize:每页显示条数," +
                    "totalPageNum:总页数,List[{}]}")
    })
    public Result getHuapinByUserId(@RequestBody Map<String, Object> map) {
        return Results.successWithData(huapinOrderService.getListByUserId(map), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/updateorder.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据订单id，更新订单状态处理,支付回调", notes = " ")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "orderCode", value = "订单Code", example = "1fsfsdf", paramType = "query", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "money", value = "支付金额", example = "1", paramType = "query", required = true, dataType = "BigDecimal")
            }
    )
    public Result updateOrderPayStatusByOrderCode(String orderCode, BigDecimal money) throws Exception {
        return Results.successWithData(userOrderService.updateOrderPayStatusByOrderId(orderCode, money), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/updatesendstatus.do", method = RequestMethod.POST)
    @ApiOperation(value = "更新配送状态处理", notes = "{\"order_userid\":1,\"current_userid\":2,\"order_id\":3}")
    public Result updateSendStatus(@RequestBody @ApiParam(value ="")Map<String,Object> mapStatus) throws  Exception{
        if(mapStatus==null || mapStatus.get("order_userid")==null || mapStatus.get("current_userid")==null || mapStatus.get("order_id")==null){
            return Results.successWithData("必传的内容为空", BaseEnums.FAILURE.code(), BaseEnums.FAILURE.desc());
        }

        return Results.successWithData(userOrderService.updateSendStatus(mapStatus), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
}
